package com.monster.servlet;

import com.alibaba.fastjson.JSONArray;
import com.monster.dao.SongDao;
import com.monster.dao.SongDaoImpl;
import com.monster.dao.UserDao;
import com.monster.dao.UserDaoImpl;
import com.monster.pojo.*;
import com.monster.service.UserService;
import com.monster.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

@WebServlet("/CSXUserServlet")
public class CSXUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        switch (opr){
            case "userPageMessage":
                showUserPage(request,response);
                break;
            case "addPlaylist":
                addPlaylist(request,response);
                break;
            case "searchSong":
                searchSong(request,response);
                break;
        }
    }

    //展示用户信息页面
    public void showUserPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取session中存储的User数据，取得用户的信息
        User user = (User)request.getSession().getAttribute("user");
        //System.out.println(user.getFans());
        //通过用户ID查询用户收藏的歌曲
        UserService userService = new UserServiceImpl();
        ArrayList<Object> list = userService.listMyLike(user.getId(),"song",1);
        ArrayList<Song> songsList = (ArrayList<Song>)(ArrayList)list;
        //通过用户ID查询用户关注的数量
        UserDao userDao = new UserDaoImpl();
        int follow = userDao.getFollowCountByUserId(user.getId());
        user.setFollow(follow);
        //通过用户ID查询粉丝的数量
        int fans = userDao.getFansCountByUserId(user.getId());
        user.setFans(fans);
        //通过用户ID查询用户收藏的歌单（点击事件触发）
        ArrayList<Object> userPlaylist = userService.listMyLike(user.getId(),"playlist",1);
        ArrayList<Playlist> userPlaylists = (ArrayList<Playlist>)(ArrayList)userPlaylist;

        //通过用户ID查询用户收藏的专辑（点击事件触发）
        ArrayList<Object> albumList = userService.listMyLike(user.getId(),"album",1);
        ArrayList<Album> userAlbumLists = (ArrayList<Album>)(ArrayList)albumList;

        //通过用户ID查询用户创建的歌单
        ArrayList<Playlist> userSetPlaylist = userService.listSetPlaylist(user.getId(),1);

        //通过用户ID查询用户关注的歌手（点击事件触发）
        ArrayList<Object> FocusSinger = userService.listFocus(user.getId(),"singer",1);
        ArrayList<Singer> userFocusSinger = (ArrayList<Singer>)(ArrayList)FocusSinger;

        //通过用户ID查询用户关注的其他用户（点击事件触发）
        ArrayList<Object> focusUser = userService.listFocus(user.getId(),"user",1);
        ArrayList<User> userFocusUser = (ArrayList<User>)(ArrayList)focusUser;

        HttpSession session = request.getSession();
        session.setAttribute("userFocusUser",userFocusUser);
        session.setAttribute("userFocusSinger",userFocusSinger);
        session.setAttribute("userSetPlaylist",userSetPlaylist);
        session.setAttribute("userAlbumLists",userAlbumLists);
        session.setAttribute("userPlaylists",userPlaylists);
        session.setAttribute("user",user);
        session.setAttribute("songsList",songsList);
        response.sendRedirect("profile.jsp");
    }

    //添加歌单
    public void addPlaylist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        //获取用户ID
        String newId = request.getParameter("id");
        Long id = Long.parseLong(newId);
        //获取用户创建的歌单名
        String playlistName = request.getParameter("playlistName");
        if(playlistName==null||playlistName.equals("")){
            out.print(false);
        }else{
            //把用户输入的歌单名 用户ID set到歌单表中
            Playlist playlist = new Playlist();
            playlist.setPlaylistName(playlistName);
            playlist.setUserId(id);
            //调用增加歌单的方法
            UserService userService = new UserServiceImpl();
            boolean result = userService.addPlaylist(playlist);
            //通过用户ID查询用户创建的歌单
            ArrayList<Playlist> userAddPlaylist = userService.listSetPlaylist(id,1);
            out.print(JSONArray.toJSON(userAddPlaylist));
        }

        out.flush();
        out.close();
    }

    public void searchSong(HttpServletRequest request, HttpServletResponse response){
        System.out.print("111111111111111");
    }
}
